package cn.stylefeng.guns.modular.time.controller;

import cn.stylefeng.guns.modular.time.entity.Transaction;
import cn.stylefeng.guns.modular.time.model.request.TransactionRequest;
import cn.stylefeng.guns.modular.time.service.TransactionService;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import cn.stylefeng.roses.kernel.rule.pojo.response.ResponseData;
import cn.stylefeng.roses.kernel.rule.pojo.response.SuccessResponseData;
import cn.stylefeng.roses.kernel.scanner.api.annotation.ApiResource;
import cn.stylefeng.roses.kernel.scanner.api.annotation.GetResource;
import cn.stylefeng.roses.kernel.scanner.api.annotation.PostResource;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Map;

@RestController
@ApiResource(name = "记账管理")
public class AccountController {

    @Resource
    private TransactionService transactionService;

    @GetResource(name = "分页查询收支记录", path = "/account/page")
    public ResponseData<PageResult<Transaction>> page(TransactionRequest request) {
        return new SuccessResponseData<>(transactionService.findPage(request));
    }

    @PostResource(name = "新增收支记录", path = "/account/add")
    public ResponseData<Void> add(@RequestBody @Validated(TransactionRequest.add.class) TransactionRequest request) {
        transactionService.add(request);
        return new SuccessResponseData<>();
    }

    @PostResource(name = "编辑收支记录", path = "/account/edit")
    public ResponseData<Void> edit(@RequestBody @Validated(TransactionRequest.edit.class) TransactionRequest request) {
        transactionService.edit(request);
        return new SuccessResponseData<>();
    }

    @PostResource(name = "删除收支记录", path = "/account/delete")
    public ResponseData<Void> delete(@RequestBody @Validated(TransactionRequest.delete.class) TransactionRequest request) {
        transactionService.delete(request);
        return new SuccessResponseData<>();
    }

    @GetResource(name = "按分类统计", path = "/account/stats/category")
    public ResponseData<List<Map<String, Object>>> statsByCategory(TransactionRequest request) {
        return new SuccessResponseData<>(transactionService.statsByCategory(request));
    }
}

